<template>
	<view>
		<!-- #ifdef MP-WEIXIN -->
		<button type="primary" open-type="getUserInfo" @getuserinfo="getUserInfo">使用微信登录</button>
		<!-- #endif -->
	</view>
</template>

<script>
	var session_key, openid, pageOptions;
	var APITOKEN = 'api2018';
	var apiServer = 'https://www.hczycm.com/api/user/';
	// var sign = require('../../commons/sign.js');
	export default {
		data() {
			return {
				code: 'asdf'
			}
		},
		onLoad: function(options) {
			console.log(options);
			pageOptions = options;
			//微信
			// #ifdef MP-WEIXIN
			uni.login({
				provider: 'weixin',
				success: function(loginRes) {
					if (loginRes.code) {
						// 这里处理获取到的code
						console.log('登录成功，code:', loginRes.code);
						// 进行下一步操作，比如调用后端接口换取用户信息
						this.code = loginRes.code
						uni.request({
							url: apiServer + 'codeToSession?code=' + this.code,
							method: 'GET',
							success: res => {
								console.log(res);
								session_key = res.data.session_key;
								openid = res.data.openid;
							}
						});
					} else {
						console.error('登录失败！' + loginRes.errMsg);
					}
				},
				fail: function(err) {
					console.error('登录接口调用失败:', err);
				}
			});
			// #endif

			// app
			// #ifdef APP-PLUS
			uni.login({
				provider: 'weixin',
				success: (res) => {
					uni.getUserInfo({
						success: function(info) {
							console.log(JSON.stringify(info));
							var sign = uni.getStorageSync('sign');
							var url = apiServer + 'applogin'
							uni.request({
								url: url,
								method: 'POST',
								header: {
									'content-type': "application/x-www-form-urlencoded"
								},
								data: {
									openid: info.userInfo.openId,
									name: info.userInfo.nickName,
									face: info.userInfo.avatarUrl,
									sign: sign
								},
								success: res => {
									console.log(JSON.stringify(res.data));
									if (res.data.status == 'ok') {
										uni.showToast({
											title: "登录成功"
										});
										uni.setStorageSync('SUID', res.data
											.user_id + '');
										uni.setStorageSync('SRAND', res.data.id +
											'');
										uni.setStorageSync('SNAME', res.data
											.nickname + '');
										uni.setStorageSync('SFACE', res.data
											.avatarurl + '');
										// 跳转
										if (options.backtype == '1') {
											console.log(options);
											uni.switchTab({
												url: options.backpage
											});
										} else {
											uni.redirectTo({
												url: options.backpage
											});
										}
									} else {
										console.log(JSON.stringify(res));
										uni.showToast({
											title: res.data
										});
									}
								},
								fail: (res) => {
									console.log(res);
								},
								complete: () => {}
							});
						},
						fail: function() {
							uni.showToast({
								title: "微信登录授权失败",
								icon: "none"
							});
						}
					})
				},
				fail: () => {
					uni.showToast({
						title: "微信登录授权失败",
						icon: "none"
					});
				}
			})
			// #endif
		},
		methods: {
			getUserInfo: function(info) {
				info = info.detail.userInfo;
				console.log(info);
				var sign = uni.getStorageSync('sign');
				uni.request({
					url: apiServer + 'wxlogin',
					method: 'POST',
					header: {
						'content-type': "application/x-www-form-urlencoded"
					},
					data: {
						openid: openid,
						name: info.nickName,
						face: info.avatarUrl,
						sign: sign
					},
					success: res => {
						console.log(res);
						if (res.data.status == 'ok') {
							setTimeout(()=>{
								// 跳转
								if (pageOptions.backtype == '1') {
									uni.switchTab({
										url: pageOptions.backpage
									});
							
								} else {
									uni.redirectTo({
										url: pageOptions.backpage
									});
								}
							},2000)
							uni.setStorageSync('SUID', res.data.user_id + '');
							uni.setStorageSync('SRAND', res.data.id + '');
							uni.setStorageSync('SNAME', res.data.nickname + '');
							uni.setStorageSync('SFACE', res.data.avatarurl + '');
						} else {
							console.log(JSON.stringify(res));
							uni.showToast({
								title: res.data
							});
						}
					},
					fail: (e) => {
						console.log(e);
					}
				});
			}
		}
	}
</script>

<style>

</style>